package com.jiaming.restaurant.api.restaurantgoods.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jiaming.restaurant.api.goods.bean.entity.Goods;
import com.jiaming.restaurant.api.goods.service.IGoodsService;
import com.jiaming.restaurant.api.restaurantgoods.bean.entity.RestaurantGoods;
import com.jiaming.restaurant.api.restaurantgoods.bean.vo.RestGoodsVO;
import com.jiaming.restaurant.api.restaurantgoods.bean.vo.RestaurantGoodsVO;
import com.jiaming.restaurant.api.restaurantgoods.mapper.RestaurantGoodsMapper;
import com.jiaming.restaurant.api.restaurantgoods.service.IRestaurantGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author dragon
 * @create 2020-12-28 17:33
 */
@Service
public class RestaurantGoodsServiceImpl extends ServiceImpl<RestaurantGoodsMapper, RestaurantGoods>
        implements IRestaurantGoodsService {

    @Autowired
    IGoodsService goodsService;

    @Override
    public List<Goods> list(RestaurantGoodsVO restaurantGoodsVO) {
        QueryWrapper<RestaurantGoods> query = Wrappers.query();
        query.eq("restaurant_id", restaurantGoodsVO.getRestaurantId());
        Integer status = restaurantGoodsVO.getStatus();
        if (status != null) {
            query.eq("status", status);
        }
        List<RestaurantGoods> list = this.list(query);
        List<Goods> goodsList = new ArrayList<>();
        for (RestaurantGoods restaurantGoods : list) {
            Goods goods = goodsService.getById(restaurantGoods.getGoodsId());
            goodsList.add(goods);
        }
        return goodsList;
    }

    @Override
    public boolean removeByGoodsIds(List<Integer> goodsIds) {
        UpdateWrapper<RestaurantGoods> wrapper = Wrappers.update();
        wrapper.in("goods_id", goodsIds);
        return this.remove(wrapper);
    }

    @Override
    public RestaurantGoods getByRestaurantIdAndGoodsId(Integer restaurantId, Integer goodsId) {
        QueryWrapper<RestaurantGoods> query = Wrappers.query();
        query.eq("restaurant_id", restaurantId).eq("goods_id", goodsId);
        return this.getOne(query);
    }

    @Override
    public RestaurantGoods getByRestaurantIdAndGoodsIdAndId(Integer id, Integer restaurantId, Integer goodsId) {
        QueryWrapper<RestaurantGoods> query = Wrappers.query();
        query.eq("restaurant_id", restaurantId).eq("goods_id", goodsId).eq("id", id);
        return this.getOne(query);
    }

    @Override
    public List<RestGoodsVO> getByRestId(RestaurantGoodsVO restaurantGoodsVO) {
        QueryWrapper<RestaurantGoods> query = Wrappers.query();
        query.eq("restaurant_id", restaurantGoodsVO.getRestaurantId());
        List<RestaurantGoods> list1 = this.list(query);
        List<RestGoodsVO> list = new ArrayList<>();
        for (RestaurantGoods restaurantGoods : list1) {
            QueryWrapper<Goods> query1 = Wrappers.query();
            query1.eq("id",restaurantGoods.getGoodsId());
            Goods one = goodsService.getOne(query1);
            RestGoodsVO restGoodsVO = new RestGoodsVO();
            restGoodsVO.setName(one.getName());
            restGoodsVO.setStatus(restaurantGoods.getStatus());
            restGoodsVO.setId(restaurantGoods.getId());
            restGoodsVO.setPrice(one.getPrice());
            list.add(restGoodsVO);
        }
        return list;
    }

}
